import json
class ProStatus():
	connector = set()

	def user_connect(self,user):
		if user not in self.connector:
			self.connector.add(user)

	def user_remove(self,user):
		self.connector.remove(user)

	def message_to_connector(self,message,binary=False,user=None):
		# logger.info(user)
		if user and user in self.connector:
			user.write_message(message,binary=binary)
		else:
			for user in self.connector:
				user.write_message(message,binary=binary)

	def trigger(self,message,user=None):
		"""向终端发消息"""
		message = {"code":0,"type":"message", "message":message}
		message = json.dumps(message)
		# logger.info(message)
		self.message_to_connector(message,user=user)

	def triggerUpdateData(self,data,user=None):
		message = {"code":0,"type":"data","list":data}
		message = json.dumps(message)
		# logger.info(message)
		self.message_to_connector(message,user=user)

	def triggerDeleteData(self,id,user=None):
		message = {"code":0,"type":"delete","id":id}
		message = json.dumps(message)
		# logger.info(message)
		self.message_to_connector(message,user=user)

	def triggerRequestHandler(self,requestHandler,user=None):
		# logger.info(requestHandler)
		# message = '%s %s'%(requestHandler.request.method,requestHandler.request.uri)
		# logger.info(requestHandler.request.headers)
		# logger.info(id(requestHandler))
		if requestHandler.request.method == 'CONNECT':
			protocol = 'https'
			request_body = None
			request_headers = requestHandler.request.headers.__dict__['_dict']
		else:
			protocol = requestHandler.request.protocol
			request_headers = requestHandler.request.headers.__dict__['_dict']
			if type(requestHandler.request.body) != str:
				request_body = requestHandler.request.body.decode('utf-8')
			else:
				request_body = requestHandler.request.body
		message = {'type':'load',
			'id': str(id(requestHandler)),
			'code':'',	# Code
			'remote_ip':requestHandler.request.remote_ip,
			'protocol':protocol,
			'method':requestHandler.request.method,
			'host':requestHandler.request.host,
			'uri':requestHandler.request.uri.replace('http://',''),
			'body_size':'0', 	# Body-Size
			'dev_info':requestHandler.request.headers['X-Dev-Info'] if 'X-Dev-Info' in requestHandler.request.headers else '',
			'request_headers': request_headers,
			'request_body': request_body,
			'response_headers':None,
			'response_body':None
		}
		message = json.dumps(message)
		self.message_to_connector(message,user=user)

	def triggerResponseHandler(self,requestHandler,response,user=None):
		# https请求暂未处理
		# logger.info(requestHandler.request)
		if type(response.body) != str:
			response_body = response.body.decode('utf-8')
		else:
			response_body = response.body
		message = {'type':'update',
			'id': str(id(requestHandler)),
			'code':response.code,	# Code
			'remote_ip':requestHandler.request.remote_ip,
			'protocol':requestHandler.request.protocol,
			'host':requestHandler.request.host,
			'uri':requestHandler.request.uri,
			'body_size':len(response.body), 	# Body-Size
			'dev_info':requestHandler.request.headers['X-Dev-Info'] if 'X-Dev-Info' in requestHandler.request.headers else '',
			'response_headers':response.headers.__dict__['_dict'],
			'response_body': response_body
		}
		# logger.info(message)
		message = json.dumps(message)
		self.message_to_connector(message,user=user)
